// 登录切换
class Switch{
    constructor(){
        this.loginL = document.querySelector(".text_left a");
        this.loginR = document.querySelector(".text_right a");
        this.Account = document.querySelector(".login_inp");
        this.Quick = document.querySelector(".login_quick");
        this.addEvent();
    }
    addEvent(){
        const _this = this;
        this.loginL.addEventListener("click",function(){
            _this.loginL.className = "active";
            _this.loginR.className = "";
            _this.Account.style.display = "block";
            _this.Quick.style.display = "none";
        })
        this.loginR.addEventListener("click",function(){
            _this.loginL.className = "";
            _this.loginR.className = "active";
            _this.Account.style.display = "none";
            _this.Quick.style.display = "block";
        })
    }
}
new Switch();
// 随机验证码图
class Vcode{
    constructor(){
        this.oimg = document.querySelector(".imgCode_img");
        this.addEvent();
    }
    addEvent(){
        const _this = this;
        this.oimg.addEventListener("click",function(){
            _this.render();
        })
    }
    render(){
        if(Math.random()<0.222){
            this.oimg.src = "./images/VerificationCode.jpg";
        }else if(Math.random()<0.444){
            this.oimg.src = "./images/VerificationCode2.jpg";
        }else if(Math.random()<0.666){
            this.oimg.src = "./images/VerificationCode3.jpg";
        }else if(Math.random()<0.888){
            this.oimg.src = "./images/VerificationCode4.jpg";
        }else if(Math.random()<1){
            this.oimg.src = "./images/VerificationCode5.jpg";
        }
    }
}
new Vcode();
// 登录请求
class Login{
    constructor(){
        this.user = document.getElementById("login_id");
        this.pass = document.getElementById("password");
        this.smt = document.getElementById("submit");
        this.url = "http://localhost:2926/api";
        this.addEvent();
    }
    addEvent(){
        this.smt.addEventListener("click",()=>{
            this.ajax();
        })
    }
    ajax(){
        ajaxGet(this.url,(res)=>{
            this.res = JSON.parse(res);
            if(this.res.code === 1){
                alert("密码错误");
                this.pass.value = "";
                this.pass.focus();
            }else if(this.res.code === 2){
                if(confirm("登录成功，是否跳转到首页")){
                    location.href = "./index.html";
                }
                localStorage.setItem("isLogin","YES");
                localStorage.setItem("username",this.res.data);
            }else if(this.res.code === 0){
                if(confirm("用户名不存在，是否跳转到注册")){
                    location.href = "./registered.html?username="+this.user.value;
                }else{
                    this.user.value = "";
                    this.user.focus();
                }
            }
        },{
            type:"login",
            username:this.user.value,
            password:this.pass.value
        })
    }
}
new Login();